package com.haojin.haojintimingmonitor.scheduledTask;


import com.haojin.haojintimingmonitor.domain.AlarmLog;
import com.haojin.haojintimingmonitor.domain.TravelVehicle;
import com.haojin.haojintimingmonitor.config.MessageInfo;
import com.haojin.haojintimingmonitor.service.AlarmLogService;
import com.haojin.haojintimingmonitor.service.VehicleTravelService;
import com.haojin.haojintimingmonitor.utils.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Component
@Slf4j
public class TimingMonitor {
    @Autowired
    private MessageInfo messageInfo;

    private TravelVehicle travelVehicle;
    private AlarmLog alarmLog;

    @Autowired
    private VehicleTravelService vehicleTravelService;
    @Autowired
    private AlarmLogService alarmLogService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Scheduled(cron  =  "0 */15 7-23 * * *")
    public void dayTimingMonitor() throws InterruptedException {
        //刚执行时的旅程信息
        TravelVehicle tempTravel = vehicleTravelService.getRecentTravel();
        log.info("车辆行程"+tempTravel);
        //刚执行时的报警日志信息
        AlarmLog tempAlarm = alarmLogService.getRecentAlarmLog();
        log.info("告警记录"+tempAlarm);

        if (travelVehicle == null ? false:travelVehicle.getTravelId().compareTo(tempTravel.getTravelId()) == 0) {
            log.info("发送短信");
            SmsUtils.sendMessage(messageInfo.getDay(),messageInfo.getTableA(),messageInfo.getApp());
        }
        if (alarmLog == null ? false:alarmLog.getId().compareTo(tempAlarm.getId()) == 0) {
            log.info("发送短信");
            SmsUtils.sendMessage(messageInfo.getDay(),messageInfo.getTableB(),messageInfo.getApp());

        }

        travelVehicle = tempTravel;
        alarmLog = tempAlarm;

    }
    @Scheduled(cron = "0 0 0-6 * * ?")
    public void nightTimingMonitor() throws InterruptedException {
        //刚执行时的旅程信息
        TravelVehicle tempTravel = vehicleTravelService.getRecentTravel();
        log.info("车辆行程"+tempTravel);
        //刚执行时的报警日志信息
        AlarmLog tempAlarm = alarmLogService.getRecentAlarmLog();
        log.info("告警记录"+tempAlarm);

        if (travelVehicle == null ? false:travelVehicle.getTravelId().compareTo(tempTravel.getTravelId()) == 0) {
            log.info("发送短信");
            SmsUtils.sendMessage(messageInfo.getDay(),messageInfo.getTableA(),messageInfo.getApp());
        }
        if (alarmLog == null ? false:alarmLog.getId().compareTo(tempAlarm.getId()) == 0) {
            log.info("发送短信");
            SmsUtils.sendMessage(messageInfo.getDay(),messageInfo.getTableB(),messageInfo.getApp());
        }

        travelVehicle = tempTravel;
        alarmLog = tempAlarm;
    }
//    @Scheduled(cron  =  "* * * * * ?")
//    public void test(){
//        log.info("车辆行程记录信息{}",vehicleTravelService.getRecentTravel());
//        log.info("告警日志记录信息{}",alarmLogService.getRecentAlarmLog());
//    }
}
